home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
manchest.lha
/
MANCHESTER
/
manchester
/
2.2
/
Integer-digitMultiplyneg.st
< prev
next >
Wrap
Text File
|
1993-07-24
|
2KB
|
49 lines
" NAME Integer-digitMultiplyneg
AUTHOR TPH@cs.man.ac.uk
FUNCTION Parc performance enhancement
ST-VERSIONS 2.2
PREREQUISITES
CONFLICTS
DISTRIBUTION world
VERSION 1.1
DATE 22 Jan 1989
SUMMARY Integer-digitMultiplyneg
The most recent version of the ParcPlace
Newsletter has a goodie for improving the performance of integer
(both Small- and Large-) multiply. This is it. Works OK for VI2.2
images; it has not been tryed with VI2.3 images. TPH
"!
'From Smalltalk-80, Version 2.2 of July 4, 1987 on 26 June 1989 at 5:23:43 am'!
!Integer methodsFor: 'private'!
digitMultiply: arg neg: ng
"Answer the result of multiplying the receiver by
the argument arg, where the sign of the result is ng."
| argSize mySize prod pl digit k acc |
argSize _ arg digitLength.
((arg digitAt: 1) = 0 and: [argSize = 1]) ifTrue: [^0].
mySize _ self digitLength.
pl _ mySize + argSize.
"Make a better guess as to whether the product will
overflow, to avoid having to copy the result."
((self digitAt: mySize) + 1) * ((arg digitAt: argSize) + 1) < 256
ifTrue: [pl _ pl - 1].
prod _ Integer new: pl neg: ng.
"prod starts out all zero."
1 to: mySize do: [:i |
acc _ 0.
(digit _ self digitAt: i) ~=0 ifTrue: [
k _ i.
"Loop invariant: 0<=acc<=16rFFFF, k=i+j-1."
1 to: argSize do: [:j |
acc _ (arg digitAt: j) * digit + (prod digitAt: k) + (acc bitShift: -8).
prod digitAt: k put: (acc bitAnd: 255).
k _ k + 1].
acc >= 256 ifTrue: [
prod digitAt: k put: (acc bitShift: -8)]]].
^prod truncated! !